<?php
namespace app\daifast\controller;
use \think\Db; 
use think\Session;


class Index extends \think\Controller
{
    public function index(){ 
         
         
        return $this->allFetch(get_defined_vars());
    }
    
    /**
     * 获取顶部菜单
     */
    public function top(){ 
    	$menuArr = Db::table('sys_menu')->order("listorder asc")->select(); 
    	input('id') and $id = input('id') or $id = $menuArr[0][id];
    	return $this->allFetch(get_defined_vars());
    }
    
    /**
     * 获取左边菜单
     */
    public function left(){
    	input('id') and $id = input('id') or $id = 1;  
    	$leftArr = Db::table('sys_menu_item')->where(['pid'=>intval($id)])->select(); 
    	return $this->allFetch(get_defined_vars());
    }
    
    /**
     * 管理员登录
     */
    public function login(){
        if(input('sbt')){ 
            $userInfo = Db('sys_admin')->where(['username'=>input('username')])->find();
            //判断账号是否存在
            if($userInfo){ 
                if($userInfo['password']==md5(input('password'))){
                    //判断账号是否被禁用
                    if($userInfo['status']=="2"){
                        
                        $this->showMsg('该账号已被禁用',url('Index/login'),'warning');
                    }else{
                       //登录成功 写入session
                       session('admin',$userInfo); 
                       $this->showMsg('登录成功',url('Index/index'));
                        
                    } 
                }else{ 
                    $this->showMsg('您输入的账号密码有误',url('Index/login'),'warning');
                } 
            }else{
                
                $this->showMsg('您输入的账号密码有误',url('Index/login'),'warning');
            }
            
            
        }
        
        return $this->allFetch(get_defined_vars());
        
    }
    
    /**
     * 退出
     */
    public function logout(){
        
        Session::delete('admin');
        $this->showMsg('退出成功',url('Index/index'));
    }
 
     
    
}
